package com.tyhy.program.cbc.mobileapi.util;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by Administrator on 2018-07-17 Describe :
 */
public abstract class AESTool {

	public final static String HEX = "0123456789ABCDEF";
	private static final int keyLenght = 16;
	private static final String defaultV = "0";

	// 加密
	public static String Encrypt(String sSrc, String sKey) {
		try {
			return toHex(encryptBytes(sSrc.getBytes("utf-8"), sKey));
		} catch (Exception ex) {
			throw new RuntimeException(ex.toString());
		}
	}

	public static byte[] encrypt(byte[] sSrc, String sKey) {
		try {
			return encryptBytes(sSrc, sKey);
		} catch (Exception ex) {
			throw new RuntimeException(ex.toString());
		}
	}

	public static byte[] encryptBytes(byte[] sSrc, String sKey)
			throws Exception {
		if (sKey == null) {
			throw new Exception("Key为空null");
		}
		// 判断Key是否为16位
		if (sKey.length() != keyLenght) {
			throw new Exception("Key长度不是" + keyLenght + "位");
		}
		byte[] raw = sKey.getBytes("utf-8");

		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); // "算法/模式/补码方式"
		cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
		byte[] encrypted = cipher.doFinal(sSrc);
		return encrypted;
	}

	// 解密
	public static String Decrypt(String sSrc, String sKey) {
		try {
			return new String(decryptBytes(toBytes(sSrc), sKey), "utf-8");
		} catch (Exception ex) {
			throw new RuntimeException(ex.toString());
		}
	}

	public static byte[] decrypt(byte[] sSrc, String sKey) {
		try {
			return decryptBytes(sSrc, sKey);
		} catch (Exception ex) {
			throw new RuntimeException(ex.toString());
		}
	}

	// 解密
	public static byte[] decryptBytes(byte[] sSrc, String sKey)
			throws Exception {
		// 判断Key是否正确
		if (sKey == null) {
			throw new Exception("Key为空null");
		}
		// 判断Key是否为16位
		if (sKey.length() != keyLenght) {
			throw new Exception("Key长度不是" + keyLenght + "位");
		}
		byte[] raw = sKey.getBytes("utf-8");

		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		cipher.init(Cipher.DECRYPT_MODE, skeySpec);
		byte[] original = cipher.doFinal(sSrc);
		return original;

	}

	/**
	 * 二进制转字符,转成了16进制 0123456789abcdefg
	 * 
	 * @param buf
	 * @return
	 */
	public static String toHex(byte[] buf) {
		if (buf == null)
			return "";
		StringBuffer result = new StringBuffer(2 * buf.length);
		for (int i = 0; i < buf.length; i++) {
			appendHex(result, buf[i]);
		}
		return result.toString();
	}

	private static void appendHex(StringBuffer sb, byte b) {
		sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
	}

	/**
	 * 把16进制转化为字节数组
	 * 
	 * @param hexString
	 * @return
	 */
	public static byte[] toBytes(String hexString) {
		int len = hexString.length() / 2;
		byte[] result = new byte[len];
		for (int i = 0; i < len; i++)
			result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2),
					16).byteValue();
		return result;
	}
}
